﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace working
{
    public abstract class CmdItem
    {
        public string path;
        public string name;
        public string text;
        public abstract bool haveChild { get; }
    }
    public abstract class ParentCmdItem : CmdItem
    {
        [Newtonsoft.Json.JsonIgnore()]
        public override bool haveChild
        {
            get
            {
                return childItems != null && childItems.Length > 0;
            }
        }
        public abstract CmdItem[] childItems { get; }
        public abstract CmdItem AddChild();
        public abstract void RemoveChild(CmdItem item);
        public abstract void MoveUpCmdItem(CmdItem index);
        public abstract void MoveDownCmdItem(CmdItem index);
    }

    public class CmdItem<T> : ParentCmdItem where T : CmdItem, new()
    {
        //public仅应对序列化开放
        public List<T> childs = new List<T>();
        [Newtonsoft.Json.JsonIgnore()]
        public override CmdItem[] childItems
        {
            get
            {
                return childs.ToArray() as CmdItem[];
            }
        }
        public override CmdItem AddChild()
        {
            T child = new T();
            if (string.IsNullOrEmpty(this.path))
            {
                child.path = name;
            }
            else
            {
                child.path = this.path + "/" + name;
            }
            childs.Add(child);
            return child;
        }
        public override void RemoveChild(CmdItem item)
        {
            if (item != null && childs.Contains(item))
            {
                childs.Remove(item as T);
            }
        }
        public void AddChild(T child)
        {
            if (string.IsNullOrEmpty(this.path))
            {
                child.path = name;
            }
            else
            {
                child.path = this.path + "/" + name;
            }
            this.childs.Add(child);
        }
        public void AddChilds(T[] childs)
        {
            for (int i = 0; i < childs.Length; i++)
            {
                if (string.IsNullOrEmpty(this.path))
                {
                    childs[i].path = name;
                }
                else
                {
                    childs[i].path = this.path + "/" + name;
                }
                this.childs.Add(childs[i]);
            }
        }

        public void RemoveChilds()
        {
            this.childs.Clear();
        }
        public void RemoveChild(string keyName)
        {
            var child = childs.Find(x => x.name == keyName);
            if (child != null)
            {
                childs.Remove(child);
            }
        }
        public override void MoveUpCmdItem(CmdItem cmdItem)
        {
            var idex = childs.IndexOf((T)cmdItem);
            if (idex > 0)
            {
                var temp = childs[idex];
                childs[idex] = childs[idex - 1];
                childs[idex - 1] = temp;
            }
        }
        public override void MoveDownCmdItem(CmdItem cmdItem)
        {
            var idex = childs.IndexOf((T)cmdItem);
            if (idex >= 0 && idex < childs.Count - 1)
            {
                var temp = childs[idex];
                childs[idex] = childs[idex + 1];
                childs[idex + 1] = temp;
            }
        }
       
}
    public class CmdRoot : CmdItem<CmdItem1> { }
    public class CmdItem1 : CmdItem<CmdItem2> { }
    public class CmdItem2 : CmdItem<CmdItem3> { }
    public class CmdItem3 : CmdItem<CmdItem4> { }
    public class CmdItem4 : CmdItem<CmdItem5> { }
    public class CmdItem5 : CmdItem<CmdItem6> { }
    public class CmdItem6 : CmdItem<CmdItem7> { }
    public class CmdItem7 : CmdItem<CmdItemLeaf> { }
    public class CmdItemLeaf : CmdItem {
        public override bool haveChild
        {
            get
            {
                return false;
            }
        }
    }
}
